//
//  ZCPaySuccessVC.m
//  ZCJZ
//
//  Created by sujeking on 2019/4/12.
//  Copyright © 2019年 jeking. All rights reserved.
//

#import "ZCPaySuccessVC.h"
#import "ZCOrderListVC.h"
#import <BlocksKit+UIKit.h>
#import "WZGPaySuccessPopVC.h"
#import "SKWebServiceErrorCode.h"
#import "SKBaseWebService.h"

@interface ZCPaySuccessVC ()
@property (weak, nonatomic) IBOutlet UIImageView *imv;

@property (weak, nonatomic) IBOutlet UIButton *homebtn;
@property (weak, nonatomic) IBOutlet UIButton *vlistbtn;
@property (nonatomic, copy) NSDictionary *couponData;
@end

@implementation ZCPaySuccessVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self netFetchCopounInfoWithOrderId];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navTitle = @"支付状态";
    UIImage *img = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e648", 75, [generalToolBox getColorWithHexadecimalColorStr:@"0xC20114"])];
    self.imv.image = img;
    
    self.homebtn.layer.cornerRadius = CGRectGetHeight(self.homebtn.bounds) / 2;
    self.homebtn.layer.masksToBounds = YES;
    self.homebtn.layer.borderWidth = 1.0f;
    self.homebtn.layer.borderColor = [generalToolBox getColorWithHexadecimalColorStr:@"0x666666"].CGColor;

    
    self.vlistbtn.layer.cornerRadius = CGRectGetHeight(self.vlistbtn.bounds) / 2;
    self.vlistbtn.layer.masksToBounds = YES;
    self.vlistbtn.layer.borderWidth = 1.0f;
    self.vlistbtn.layer.borderColor = [generalToolBox getColorWithHexadecimalColorStr:@"0xC20114"].CGColor;

    
    [self.homebtn bk_addEventHandler:^(id sender) {
        [self.tabBarController setSelectedIndex:0];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
    } forControlEvents:UIControlEventTouchUpInside];
	
    
    [self.vlistbtn bk_addEventHandler:^(id sender) {
        ZCOrderListVC *vc = [[ZCOrderListVC alloc] init];
        NSMutableArray *vcs = [[self.navigationController childViewControllers] mutableCopy];
        [vcs removeObject:self];
        [vcs addObject:vc];
        [self.navigationController setViewControllers:vcs animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    
    if (self.gift_code != nil) {
        NSString *content = [NSString stringWithFormat:@"邀请码:%@",self.gift_code];
        [self showAlert:@"提示" content:content actionStr1:@"复制邀请码" style:UIAlertActionStyleDefault actionAc1:^(UIAlertAction * _Nonnull action) {
            UIPasteboard *pb = [UIPasteboard generalPasteboard];
            pb.string = self.gift_code;
            UIApplication *app = [UIApplication sharedApplication];
            [app.keyWindow makeToast:@"复制成功" duration:2.0 position:CSToastPositionCenter];
        } actionStr2:@"进入邀请码列表" style:UIAlertActionStyleDefault actionAc:^(UIAlertAction * _Nonnull action) {
//            ZCInviteOpenShopCodeListVC *vc = [[ZCInviteOpenShopCodeListVC alloc] init];
//            NSMutableArray *vcs = [[self.navigationController childViewControllers] mutableCopy];
//            [vcs removeObject:self];
//            [vcs addObject:vc];
//            [self.navigationController setViewControllers:vcs animated:YES];
        }];
    }
}

- (void)basePopAction {
    NSMutableArray *vcs = [[self.navigationController childViewControllers] mutableCopy];
    for (UIViewController *vc in self.navigationController.childViewControllers) {
        if ([vc isKindOfClass:NSClassFromString(@"ZCOrderPayVC")]
            ||[vc isKindOfClass:NSClassFromString(@"ZCOrderCheckVC")]) {
            [vcs removeObject:vc];
        }
    }
    [vcs removeObject:self];
    UIViewController *lastvc = vcs.lastObject;
    [self.navigationController popToViewController:lastvc animated:YES];

}

- (void)loadPopVC {
    WZGPaySuccessPopVC *vc = [[WZGPaySuccessPopVC alloc] init];
    vc.amountstr = [self.couponData valueForKey:@"money"];
    vc.subtitle = [self.couponData valueForKey:@"type"];
    [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    __weak typeof(self) weakSelf = self;
    [vc setBtnClickCallback:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    [self.view.window.rootViewController presentViewController:vc animated:YES completion:nil];
}

// MARK: - NET

/// 获取优惠券信息并提示
- (void)netFetchCopounInfoWithOrderId {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.orderId forKey:@"order_id"];
    __weak typeof(self) weakSelf = self;
    [SKBaseWebService getRequest:@"/v1/coupon/order-coupon" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        NSArray *arr = (NSArray *)data;
        if (arr == nil || arr.count == 0) {
            return;
        }
        NSDictionary *dict = arr.firstObject;
        weakSelf.couponData = dict;
        [weakSelf loadPopVC];
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        NSLog(@"error");
    }];

}



@end
